var express = require('express');
var router = express.Router();
var { uploadFile } = require("../utils/upload.js");
const { get,update,del ,addUser,search} = require("../service/usersService.js")


router.post('/getUsers', async function (req, res, next) {
  const data = await get();
  res.send(data);
});

router.post('/updateUsers', async function (req, res, next) {
  const {parameter} = req.body;
  const data = await update(parameter);
  res.send(data);
});
router.post('/addUser', async function (req, res, next)  { 
  const {parameter} = req.body;
  console.log(parameter);
  
  const data = await addUser(parameter);
  res.send(data);
});

router.post('/deleteUsers', async function (req, res, next) {
  const {_id} = req.body;
  const data = await del(_id);
  res.send(data);
});


router.post('/upload', async function (req, res, next) {
  console.log("in");
  const { data, success } = await uploadFile(req, {
      "fileType": "users",
      "path": "./public/imga/"
  })
  success ? res.send({ url: data.url }) : res.send({})
});

router.post('/searchUser', async function (req, res, next) {
  const {parameter} = req.body;
  const data = await search(parameter);
  res.send(data);
});

module.exports = router;
